package jia.temp;

import jia.game.AreaViewport;
import jia.game.DialoguePanel;
import jia.game.MainButtonPanel;
import jia.game.PlayerCharacterList;
import jia.game.Viewport;
import jia.core.GameStateManager;
import jia.core.InputProcessor;
import jia.core.UiManager;
import jia.core.Module;
import jia.core.event.KeypressUiEvent;
import jia.core.event.UiEvent;
import jia.core.processors.BasicInputProcessor;
import jia.core.state.BasicGameStateManager;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class MazeInterface extends JFrame implements UiManager, KeyListener {
    private GameStateManager mgr;
    private InputProcessor input;
    private Viewport view;
    private Viewport areaView;
    private boolean areaViewOn = false;
    
    //====================================================
    /**
     * @param args
     */
    public static void main(String[] args) {
        new MazeInterface().setVisible(true);
    }
    
    //====================================================
    /**
     */
    public MazeInterface() {
        setTitle("MAZE!");
        setSize(180, 220);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(false);
        
        view = new MazeViewport();
        getContentPane().add(view);
        
        areaView = new AreaViewport();
        
        Module m = new MazeModule();
        GameStateManager gsManager = new BasicGameStateManager();
        registerGameState(gsManager);
        mgr.getPlayerState().setCurrentModule(m);
        mgr.getPlayerState().setPosition(m.getDefaultEntryPoint().getEntryPosition());
        mgr.getPlayerState().setFacing(m.getDefaultEntryPoint().getEntryFacing());
        mgr.registerUiManager(this);
        
        registerInputProcessor(new BasicInputProcessor());
        input.registerGameState(mgr);
        
        mgr.notifyPlayerStateChanged();
        addKeyListener(this);
    }
    
    //====================================================
    /**
     */
    private void toggleView() {
        if (areaViewOn) {
            getContentPane().remove(areaView);
            getContentPane().add(view);
        } else {
            getContentPane().remove(view);
            getContentPane().add(areaView);
        }
        
        areaViewOn = !areaViewOn;
        
        repaint();
    }
    
    //====================================================
    /**
     *
     */
    public void registerInputProcessor(InputProcessor ip) {
        input = ip;
    }
    
    //====================================================
    /**
     *
     */
    public void notifyNeedUpdate() {
        view.updateGameState(mgr);
        areaView.updateGameState(mgr);
    }
    
    //====================================================
    /**
     *
     */
    public JComponent getUiPanel() {
        return null;
    }
    
    //====================================================
    /**
     *
     */
    public void registerGameState(GameStateManager gs) {
        mgr = gs;
    }
    
    public void keyPressed(KeyEvent arg0) {
        if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE) {
            dispose();
            return;
        }
        if (arg0.getKeyCode() == KeyEvent.VK_F2) {
            toggleView();
        }
        UiEvent keyEvt = new KeypressUiEvent(arg0.getKeyCode());
        input.processUiEvent(keyEvt);
    }
    
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    
    /**
     * @return
     */
    public DialoguePanel getDialoguePane() {
        return null;
    }
    
    public PlayerCharacterList getCharacterList() {
        return null;
    }
    
    /**
     * @return
     */
    public MainButtonPanel getButtonPanel() {
        return null;
    }
    
    public void setDialogText(String text) {
        
    }
    
    //====================================================
    /**
     * @param arg0
     */
    public void keyTyped(KeyEvent arg0) {
        
    }
}
